package com.hzit.hzb.core.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzit.common.result.R;
import com.hzit.hzb.core.pojo.entity.UserInfo;
import com.hzit.hzb.core.pojo.entity.UserLoginRecord;
import com.hzit.hzb.core.pojo.vo.UserInfoQuery;
import com.hzit.hzb.core.service.UserInfoService;
import com.hzit.hzb.core.service.UserLoginRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "会员管理")
@RestController
@RequestMapping("/admin/core/userInfo")
@Slf4j
public class AdminUserInfoController {
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private UserLoginRecordService recordService;

	// 1. 列表查询用户
	@ApiOperation("获取会员分页列表")
	@PostMapping("/list/{page}/{limit}")
	public R listPage(@PathVariable("page")
	                  @ApiParam(value="当前页",required = true,defaultValue = "1") Integer page,
	                  @PathVariable("limit")
	                  @ApiParam(value = "每页大小",required = true,defaultValue = "2") Integer limit,
	                  @RequestBody
	                  @ApiParam(value="会员查询参数",required = false) UserInfoQuery query){
		IPage<UserInfo> userInfoIPage = userInfoService.listPage(page,limit,query);
		return R.ok().data("pageModel",userInfoIPage);
	}

	// 2. 修改用户状态
	@ApiOperation("修改用户状态")
	@PutMapping("/lock/{id}/{status}")
	public R lock(@PathVariable("id")
			      @ApiParam(value="用户id",required = true) Long id,
	              @PathVariable("status")
			      @ApiParam(value="用户状态",required = true) Integer status ){
		userInfoService.lock(id,status);
		return R.ok().message(status == 1 ? "解锁成功!" : "锁定成功！");
	}

	// 3. 根据用户id查询用户登录日志
	@ApiOperation("显示用户登录日志")
	@GetMapping("/userLoginRecord/{id}")
	public R userLoginRecord(@PathVariable("id") Long id){
		List<UserLoginRecord> userLoginRecords = recordService.userLoginRecord(id);
		return R.ok().data("loginRecordList",userLoginRecords);
	}
}
